package com.maoye.ossp.biz.brand.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.maoye.ossp.biz.brand.entity.Supplier;
import com.maoye.ossp.biz.brand.service.ISupplierService;
import com.maoye.ossp.biz.brand.service.ISupplierUserService;
import com.maoye.sc.auth.core.authenticator.AccountSubject;
import com.maoye.sc.auth.core.authenticator.DefaultAccountSubject;
import com.maoye.sc.auth.local.token.AccountToken;

@Controller
@RequestMapping("/biz/brand/supplierUser/")
public class SupplierUserController {
	@Resource
	private ISupplierUserService supplierUserService;
	AccountSubject accountSubject;
	@Resource
	private ISupplierService supplierService;
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	@RequestMapping(value = "register")
	public ModelAndView register(Supplier supplier,Model model,RedirectAttributes redirectAttributes) {
		ModelAndView modelAndView = new ModelAndView();
		try {
			String msg = supplierUserService.saveRegister(supplier);
			if (null != msg && !"".equals(msg))
			{
				modelAndView.addObject("msg",msg);
	            modelAndView.setViewName("/biz/brand/supplierUser/register");
			}else
			{
				AccountToken at = new AccountToken();
				at.setAccount(supplier.getUsername());
				at.setPassword(supplier.getPassword());
			    accountSubject=new DefaultAccountSubject();
			    accountSubject.login(at);
				modelAndView.setViewName("redirect:/index");
			}
		}catch (Exception e) {
			logger.error("注册失败",e);
            modelAndView.addObject("msg","注册失败");
            modelAndView.setViewName("/biz/brand/supplierUser/register");
        }
		return modelAndView;
	}

	@RequestMapping(value = "toRegister")
	public String toRegister(Supplier supplier, Model model) {
		return "/biz/brand/supplierUser/register";
	}
	
	@RequestMapping(value = "{id}/checkSupplierUser")
	public void checkSupplierUser(@PathVariable String id, Model model, HttpServletRequest request,
			HttpServletResponse response) throws Exception 
	{
		boolean flag = supplierUserService.checkSupplierUser(id);
        response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();
		out.write("{\"isOpen\":\""+flag+"\"}");
	}
	
	@RequestMapping(value = "openUser")
	public void openUser(HttpServletRequest request,HttpServletResponse response) throws Exception
	{ 
		   String username = request.getParameter("username");
		   String password = request.getParameter("password");
		   String supplierId = request.getParameter("supplierId");
		   Supplier supplier = supplierService.getBaseInfoById(Long.valueOf(supplierId));
		   String returnVal = supplierUserService.openUser(username, password, supplier);
		   response.setCharacterEncoding("utf-8");
		   response.setContentType("text/plain");
		   PrintWriter out = response.getWriter();
		   out.write(returnVal);
	}
	
	@RequestMapping(value = "batchOpenUser")
	public void batchOpenUser(HttpServletRequest request,HttpServletResponse response) throws Exception
	{ 
		List<Supplier> supplierList = supplierService.getNotOpenUserList();
	    String returnVal = supplierUserService.openUser(supplierList);
	   	response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();
		out.write(returnVal);
	}
}